package com.cskaoyan.service;

import com.cskaoyan.bean.Brand;
import com.cskaoyan.bean.ItemsListVO;
import com.cskaoyan.mapper.BrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    BrandMapper brandMapper;

    @Override
    public ItemsListVO<Brand> queryBrandList(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        PageHelper.startPage(page,limit);

        String strId = null;
        if (id != null) {
            strId = id.toString();
            System.out.println(strId);
        }
        List<Brand> brandList = brandMapper.selectBrandList(strId, name, sort, order);


        PageInfo pageInfo = new PageInfo(brandList);
        long total = pageInfo.getTotal();

        ItemsListVO<Brand> brandListVO = new ItemsListVO<>();
        brandListVO.setItems(brandList);
        brandListVO.setTotal(total);

        return brandListVO;
    }

    @Override
    public Brand createBrand(Brand brand) {
        brand.setSortOrder((byte) 50);
        Date date = new Date();
        brand.setAddTime(date);
        brand.setUpdateTime(date);
        brand.setDeleted(false);
        brandMapper.insert(brand);
        return brand;
    }

    @Override
    public Brand updateBrand(Brand brand) {
        brand.setUpdateTime(new Date());
        brandMapper.updateByPrimaryKey(brand);
        return brand;
    }

    @Override
    public int deleteBrand(Brand brand) {
        int delete = brandMapper.deleteByPrimaryKey(brand.getId());
        return delete;
    }
}
